package com.js.nickcar.base;


import com.zyzd.commontools.log.TLog;

import retrofit2.adapter.rxjava.HttpException;
import rx.Subscriber;

/**
 * Created by 李宗源 on 2017/4/25.
 */

public abstract class ApiCallback<T> extends Subscriber<T> {

    public abstract void onSuccess(T model);

    public abstract void onFailure(String msg);

    public abstract void onFinish();

    @Override
    public void onError(Throwable e) {
        if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            int code = httpException.code();
            String msg = httpException.getMessage();
            TLog.d("zyzd", "ApiCallback>>onError--> code=" + code );
            TLog.d("zyzd", "ApiCallback>>onError--> msg" + msg);
            if (code == 504) {
                msg = "网络不给力";
            }
            if (code == 502 || code == 404) {
                msg = "服务器异常，请稍后再试";
            }
            onFailure(msg);
        } else {
            onFailure(e.getMessage());
        }
        onFinish();
    }

    @Override
    public void onNext(T data) {
        onSuccess(data);
    }

    @Override
    public void onCompleted() {
        onFinish();
    }
}
